package com.itheima.aop;

import lombok.extern.slf4j.Slf4j;
import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.*;
import org.springframework.core.annotation.Order;
import org.springframework.stereotype.Component;

import java.util.Arrays;

/**
 * @author wuJiaWei
 * @version 1.0
 */
@Order(2)
@Component
@Aspect
@Slf4j
public class MyAspect1 {
    /**
     * 切入点方法（公共的切入点表达式）
     */
    //@Pointcut("execution(* com.itheima.service.impl.DeptServiceImpl.*(..))")
    @Pointcut("@annotation(com.itheima.aop.MyLog)")
    public void pt(){}
    /**
     * 前置通知
     */
    @Before("pt()")
    public void before(JoinPoint joinPoint){
        log.info(joinPoint.getSignature()+"before...:{}", Arrays.toString(joinPoint.getArgs()));
    }

    /**
     * 环绕通知
     */
    @Around("pt()")
    public Object around(ProceedingJoinPoint proceedingJoinPoint) throws Throwable {
        log.info("around before:{}", Arrays.toString(proceedingJoinPoint.getArgs()));
        Object proceed = proceedingJoinPoint.proceed();
        log.info("around after:{}",proceed);
        return proceed;
    }

    /**
     * 后置通知
     */
    @After("pt()")
    public void after(JoinPoint joinPoint){
        log.info(joinPoint.getSignature()+"after...:{}", Arrays.toString(joinPoint.getArgs()));
    }

    /**
     * 返回后通知（程序在正常执行的情况下，会执行的后置通知）
     */
    @AfterReturning("pt()")
    public void afterReturning(JoinPoint joinPoint){
        log.info(joinPoint.getSignature()+"afterReturning...:{}", Arrays.toString(joinPoint.getArgs()));
    }

    /**
     * 异常通知（程序在出现异常的情况下，执行的后置通知）
     */
    @AfterThrowing("pt()")
    public void afterThrowing(JoinPoint joinPoint){
        log.info(joinPoint.getSignature()+"afterThrowing...:{}", Arrays.toString(joinPoint.getArgs()));
    }

}
